package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.IEPSDAO;
import com.bit.cfdi.web.domain.IEPS;
import com.bit.cfdi.web.domain.Negocio;

@Repository
public class IEPSDAOImpl extends HibernateDaoSupport implements IEPSDAO {

	@Autowired
	public void init(final SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<IEPS> getAll() {
		List<IEPS> ieps = getHibernateTemplate().find(
		" from Ieps as ieps");		
		return ieps;
	}

	@Override
	@Transactional public void save(IEPS ieps) {
		getHibernateTemplate().saveOrUpdate(ieps);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional  public List<IEPS> getAllByNegocio(Negocio negocio) {
		List<IEPS> ieps = getHibernateTemplate().find(
		" from Ieps as ieps " +
		" where ieps.negocio = ?", negocio);		
		return ieps;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public IEPS getByDescAndNegocio(
			String descripcion, Negocio negocio) {
		List<IEPS> ieps = getHibernateTemplate().find(
				" from Ieps as ieps " +
				" where ieps.negocio = ? " +
				" and ieps.producto = ?", negocio, descripcion);		
				return ieps.size()>0?ieps.get(0):null;
	}

}